<?php
// +----------------------------------------------------------------------
// |欧阳长空 2018/5/28 微信model
// +----------------------------------------------------------------------

namespace Common\Model;

use Think\Model;


/**
 * 学员基础模型
 */
class WechatModel extends Model
{
 	private $appId; 
    private $appSecret;
    private $access_token;
    private $scope;
    //
    public function __construct() {
	  $config = C("QY");
	  $this->appId=$config['APP_ID'];
	  $this->appSecret=$config['APP_SECRET'];
      $this->access_token = $this->getAccessToken();
      
    }
	public function getUser(){
		$code=$this->getCode();
		//获取openid
		$r=$this->getWebAccessToken($code);
		if($r){
			$wxuser=$this->getUserInfo();
		}
		return $wxuser;
	}
	//获取code
	public function getCode(){
		if(I("code")){
			$code=I("code");
			return $code;
		}else{
			$uri=$this->getRedirectUri();
			$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appId."&redirect_uri=".$uri."&response_type=code&scope=snsapi_userinfo&state=STATE";
			header("location:".$url);
		}
	}
	//获取网页授权的access_token
	public function getWebAccessToken($code){
		$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code';
		$res = $this->getData($get_token_url);
		$jres = json_decode($res,true);
		if($jres['errcode']){
			die($jres['errmsg']);
		}else{
			$this->access_token=$jres['access_token'];
			$this->openid=$jres['openid'];
			$this->scope=$jres['scope'];
			return true;
		}
	}
	
	//拉取用户信息
	public function getUserInfo(){
		$url="http://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$this->openid."&lang=zh_CN";
		$res = $this->getData($url);
		$jres = json_decode($res,true);
		//如果有错误就刷新access_token
		
		//错误--》删除用户表示 重新获得授权
		
		//正确返回用户数据
		return $jres;
	}
	
	//获取当前页面的uril
	public function getRedirectUri(){
		$pageURL="http";
		if($_SERVER['HTTPS']=="on"){
			$pageURL.="s";
		}
		$pageURL.="://";
		if($_SERVER['SERVER_PORT']!="80"){
			$pageURL.=$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
		}else{
			$pageURL.=$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
	//
	function getAccessToken(){
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
		$res = $this->getData($url);
		$jres = json_decode($res,true);
		$access_token = $jres['access_token'];
		return $access_token;
	}
	//获取当前用户的信息
	public function getOpenId($openid){
		$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=OPENID&lang=zh_CN";
		$url=str_replace("OPENID",$openid,$url);
		$res = $this->getData($url);
		$jres = json_decode($res,true);
		return $jres;
	}
	function getData($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	function postData($url,$data){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$tmpInfo = curl_exec($ch);
		if (curl_errno($ch)) {
			return curl_error($ch);
		}
		curl_close($ch);
		return $tmpInfo;
	}
}